/**
 * 创建人:  @author huangpeng    
 * 创建时间:  2022年04月10日 00:24
 * 项目名称:  lemon-blog
 * 文件名称:  IBlAboutServiceImpl
 * 文件描述:  @Description: about的service实现
 * 公司名称:  HBlog
 *
 * All rights Reserved, Designed By HBlog 
 * @Copyright: 2021-2022
 *
 */
package com.lemon.blog.system.service.impl;

import com.lemon.blog.system.constants.LemonConstants;
import com.lemon.blog.system.domain.BlAbout;
import com.lemon.blog.system.enums.CommonEnums;
import com.lemon.blog.system.mapper.BlAboutMapper;
import com.lemon.blog.system.service.IBlAboutService;
import com.lemon.common.security.service.BaseServiceImpl;
import com.lemon.common.security.utils.OptEntityUtils;
import com.lemon.common.core.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 包名称：com.lemon.system.service.impl
 * 类名称：IBlAboutServiceImpl
 * 类描述：about的service实现
 * 创建人：@author huangpeng 
 * 创建时间：2022年04月10日 00:24
 */
@Service
public class IBlAboutServiceImpl extends BaseServiceImpl<BlAbout> implements IBlAboutService {

    @Autowired
    private BlAboutMapper aboutMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(BlAbout about) {
        about.setAboutRead(0);
        about.setVersion(1);
        about.setEnable(LemonConstants.INT_ZERO);
        OptEntityUtils.preInsert(about);
        return aboutMapper.insert(about);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BlAbout read() {
        BlAbout about = aboutMapper.getAbout();
        if (about == null) {
            return null;
        }
        // 更新阅读数
        about.setAboutRead(about.getAboutRead() + 1);
        aboutMapper.updateByPrimaryKeySelective(about);
        return about;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void enableById(Long id) {
        // 查询是否已经存在启用的关于我的
        BlAbout about = aboutMapper.getAbout();
        if (about != null) {
            throw new ServiceException(String.format("当前已存在启用中的关于我的%1$s", about.getAboutTitle()));
        }
        about = aboutMapper.selectByPrimaryKey(id);
        about.setEnable(CommonEnums.ENABLED.getCode());
        aboutMapper.updateByPrimaryKeySelective(about);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void disableById(Long id) {
        BlAbout about = aboutMapper.selectByPrimaryKey(id);
        about.setEnable(CommonEnums.NOT_ENABLE.getCode());
        aboutMapper.updateByPrimaryKeySelective(about);
    }

    @Override
    public List<BlAbout> selectAboutList(BlAbout about) {
        return aboutMapper.selectAboutList(about);
    }

}
